<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow = true" type="primary" round plain
        >新增</el-button
      >
    </div>
    <el-table :data="specsList" style="width: 100%">
      <el-table-column prop="id" label="编号" min-width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="规格名称" min-width="180">
      </el-table-column>
      <el-table-column prop="attrs" label="规格可选值" min-width="180">
        <template v-slot="props">
          <el-button
            size="mini"
            plain
            type="primary"
            :key="index"
            v-for="(item, index) in props.row.attrs"
            >{{ item }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="props">
          <el-button
            v-if="props.row.status === 1"
            size="mini"
            plain
            type="primary"
            >正常</el-button
          >
          <el-button v-else size="mini" plain type="warning">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button type="primary" @click="edit(props.row)" size="mini" round plain>编辑</el-button>
        <el-button @click="del(props.row.id)" type="warning" size="mini" round plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :page-size="size"
      background
      layout="prev, pager, next"
      :total="specsTotal"
      @current-change="pageChange"
    >
    </el-pagination>
    <!-- 调用子组件 -->
    <Dialog ref="dialog" :info="info" @specsUpdate="specsUpdate" />
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from "./Dialog";
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  components: { Dialog },
  data() {
    return {
      info: {
        // 控制对话框组件显示状态
        isShow: false,
      },
      // 页码
      page: 1,
      // 每页显示数条数
      size: 1,
    };
  },
  computed: {
    ...mapState("specs", ["specsList", "specsTotal"]),
  },
  methods: {
    ...mapActions("specs", ["getSpecsListAction", "getSpecsTotalAction"]),
    // 分页按钮点击事件处理函数
    pageChange(page) {
      // 1-更新页码
      this.page = page;
      // 2-根据最新的页码请求数据
      this.getSpecsListAction({ page: this.page, size: this.size });
    },
    // 自定义事件specsUpdate的处理函数
    specsUpdate() {
      this.getSpecsListAction({ page: this.page, size: this.size });
      this.getSpecsTotalAction();
    },
    // 删除
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/specsdelete',{id}).then(res=>{
          if(res.code===200){
              // 提示
              this.$message.success(res.msg);
              // 请求上一页的数据:
              // 1-每页只有一条数据
              // 2-当前位于最后一页, 并且最后一页之后一条数据
              if(this.size===1 || Math.ceil(this.specsTotal/this.size)===this.page&&this.specsTotal%this.size===1){
                 this.page-=1;
                 if(this.page<1){
                   this.page=1;
                 }
              }
              // 更新数据
              this.specsUpdate();
          }else{
              this.$message.error(res.msg);
          }
        });
      });
    },
    // 编辑按钮点击事件处理函数
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的规格信息传递给子组件做默认展示
      this.$refs.dialog.setFormData(row);
    }
  },
  created() {
    this.specsUpdate();
  },
};
</script>

<style>
</style>